package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

import java.util.Date;
import java.util.List;

/**
 * Created by quxingyu on 2015/8/6.
 */
public class Testpaper extends Model<Testpaper> {
    public static final Testpaper dao = new Testpaper();

    /**
     * 保存试卷
     * @param exam
     * @param questions
     * @param studentId
     */
    public void saveTestPaper(Exam exam,List<Question> questions,Long studentId){
        String name = exam.getStr("title")+"--"+Student.dao.findById(studentId).getStr("name");
        dao.set("name",name);
        dao.set("limited_time",exam.getInt("limited_time"));
        dao.set("pattern","3");
        dao.set("target_id",exam.getLong("id"));
        dao.set("target_type",exam.getStr("target_type"));
        dao.set("status",0);
        dao.set("score",exam.getInt("score"));
        dao.set("item_count",exam.getInt("item_count"));
        dao.set("created_userid",Student.dao.findById(studentId).getLong("user_id"));
        dao.set("created_time",new Date());
        dao.set("single_question_num",exam.getInt("rand_single_num"));
        dao.set("multi_question_num",exam.getInt("rand_multi_num"));
        dao.set("determine_question_num",exam.getInt("rand_choose_num"));
        dao.set("fill_blank_question_num",exam.getInt("rand_fill_num"));
        dao.set("single_question_score",exam.getInt("rand_single_score"));
        dao.set("multi_question_score",exam.getInt("rand_multi_score"));
        dao.set("determine_question_score",exam.getInt("rand_choose_score"));
        dao.set("fill_blank_question_score", exam.getInt("rand_fill_score"));
        dao.set("student_id", studentId);
        dao.remove("id").save();
        for(int i = 0;i<questions.size();i++) {
            TestpaperItem.dao.set("testpaper_id",dao.getLong("id"));
            TestpaperItem.dao.set("seq",i);
            TestpaperItem.dao.set("question_id",questions.get(i).get("id"));
            TestpaperItem.dao.set("question_type",questions.get(i).getStr("type"));
            TestpaperItem.dao.set("score", questions.get(i).get("score"));
            TestpaperItem.dao.remove("id").save();
        }
        TestpaperResult.dao.set("testpaper_id",dao.getLong("id"));
        TestpaperResult.dao.set("student_id",studentId);
        TestpaperResult.dao.set("score",0);
        TestpaperResult.dao.set("right_count",0);
        TestpaperResult.dao.set("limited_time",exam.getInt("limited_time")*60);
        TestpaperResult.dao.set("right_count",0);
        TestpaperResult.dao.set("target_type",exam.getStr("target_type"));
        TestpaperResult.dao.set("target_times",0);
        TestpaperResult.dao.set("target_id",exam.getLong("id"));
        TestpaperResult.dao.set("begin_time",new Date());
        TestpaperResult.dao.remove("id").save();
    }

    /**
     * 保存
     * @param test
     * @param questions
     * @param studentId
     */
    public void saveTestPaper(Test test,List<Question> questions,Long studentId){
        String name = test.getStr("title")+"--"+Student.dao.findById(studentId).getStr("name");
        dao.set("name",name);
        dao.set("limited_time",test.get("limited_time"));
        dao.set("pattern","3");
        dao.set("target_id",test.get("id"));
        dao.set("target_type",test.getStr("target_type"));
        dao.set("status",0);
        dao.set("score",test.get("score"));
        dao.set("item_count",test.get("item_count"));
        dao.set("created_userid",Student.dao.findById(studentId).getLong("user_id"));
        dao.set("created_time",new Date());
        dao.set("single_question_num",test.getInt("rand_single_num"));
        dao.set("multi_question_num",test.getInt("rand_multi_num"));
        dao.set("determine_question_num",test.getInt("rand_choose_num"));
        dao.set("fill_blank_question_num",test.getInt("rand_fill_num"));
        dao.set("single_question_score",test.getInt("rand_single_score"));
        dao.set("multi_question_score",test.getInt("rand_multi_score"));
        dao.set("determine_question_score", test.getInt("rand_choose_score"));
        dao.set("fill_blank_question_score", test.getInt("rand_fill_score"));
        dao.set("student_id", studentId);
        dao.remove("id").save();
        for(int i = 0;i<questions.size();i++) {
            TestpaperItem.dao.set("testpaper_id",dao.getLong("id"));
            TestpaperItem.dao.set("seq",i);
            TestpaperItem.dao.set("question_id",questions.get(i).get("id"));
            TestpaperItem.dao.set("question_type",questions.get(i).getStr("type"));
            TestpaperItem.dao.set("score", questions.get(i).get("score"));
            TestpaperItem.dao.remove("id").save();
        }
        TestpaperResult.dao.set("testpaper_id",dao.getLong("id"));
        TestpaperResult.dao.set("student_id",studentId);
        TestpaperResult.dao.set("score",0);
        TestpaperResult.dao.set("right_count",0);
        int limited_time = Integer.parseInt(test.get("limited_time").toString());
        TestpaperResult.dao.set("limited_time",limited_time * 60);
        TestpaperResult.dao.set("right_count",0);
        TestpaperResult.dao.set("target_type",test.getStr("target_type"));
        TestpaperResult.dao.set("target_times",0);
        TestpaperResult.dao.set("target_id",test.getLong("id"));
        TestpaperResult.dao.set("begin_time",new Date());
        TestpaperResult.dao.remove("id").save();
    }

    /**
     * 获取考试次数
     * @param studentId
     * @param targetId
     * @param targetType
     * @return
     */
    public Long getExamCount(Long studentId,Long targetId,String targetType){
        return Db.queryLong("select count(t.id) from t_testpaper t " +
                "where t.target_type = ? and t.target_id = ? and t.student_id =?",targetType,targetId,studentId);
    }

    public List<Testpaper> getList(Long studentId,Long targetId,String targetType){
        return dao.find("select t.* from t_testpaper t where t.target_type = ? and t.target_id = ? and t.student_id =?",targetType,targetId,studentId);
    }
 }
